<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    top="10px"
    @closed="dialogClose"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="字典项编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="字典项编码"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="dataForm.description" placeholder="描述"></el-input>
      </el-form-item>
      <el-form-item label="显示序列" prop="sequence">
        <template :slot-scope="scope">
            <el-input-number
            v-model="dataForm.sequence"
            controls-position="right"
             :min="0" :max="10"></el-input-number>
        </template>
      </el-form-item>
     <el-form-item label="所属字典分类" prop="categroyPath">
        <category-cascader :categroyPath.sync="categroyPath"></category-cascader>
      </el-form-item>
      <el-form-item label="启用状态" prop="active">
        <!-- <el-input v-model="dataForm.active" placeholder="0：不启用，1：启用"></el-input> -->
        <template :slot-scope="scope">
          <el-switch
            v-model="dataForm.active"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="启用"
            inactive-text="不启用"
            :inactive-value="0"
            :active-value="1"
          >
          </el-switch>
        </template>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import CategoryCascader from './components/category-cascader'
export default {
    components:{CategoryCascader},
  data() {
    return {
      visible: false,
      categroyPath: 0,
      dataForm: {
        id: 0,
        code: "",
        dictCode: "",
        description: "",
        active: "",
        sequence: "",
        editable: 1,
        parentId: 0,
        addTime: "",
        addUser: "",
        editTime: "",
        editUser: "",
      },
      dataRule: {
        code: [
          { required: true, message: "字典项编码不能为空", trigger: "blur" },
        ],
        dictCode: [
          { required: true, message: "字典code不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "描述不能为空", trigger: "blur" },
        ],
        active: [
          {
            required: true,
            message: "0：不启用，1：启用不能为空",
            trigger: "blur",

          },
        ],
        sequence: [
          { required: true, message: "显示序列不能为空", trigger: "blur" },
        ],
        editable: [
          {
            required: true,
            message:
              "可编辑状态：0-不可编辑   1-可编辑 继承自tb_base_dict不能为空",
            trigger: "blur",
          },
        ],

        parentId: [
          { required: true, message: "上级字典项id不能为空", trigger: "blur" },
        ],
        addTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        addUser: [{ required: true, message: "不能为空", trigger: "blur" }],
        editTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" },
        ],
        editUser: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/tbbasedictitem/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
              console.log(data);
            if (data && data.code === 0) {
              this.dataForm.code = data.tbBaseDictitem.code;
              this.dataForm.dictCode = data.tbBaseDictitem.dictCode;
              this.dataForm.description = data.tbBaseDictitem.description;
              this.dataForm.active = data.tbBaseDictitem.active;
              this.dataForm.sequence = data.tbBaseDictitem.sequence;
              this.dataForm.editable = data.tbBaseDictitem.editable;
              this.dataForm.parentId = data.tbBaseDictitem.parentId;
              this.dataForm.addTime = data.tbBaseDictitem.addTime;
              this.dataForm.addUser = data.tbBaseDictitem.addUser;
              this.dataForm.editTime = data.tbBaseDictitem.editTime;
              this.dataForm.editUser = data.tbBaseDictitem.editUser;

              //查出categroyPath的对应的
              this.categroyPath =  data.tbBaseDictitem.parentId;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/tbbasedictitem/${
                !this.dataForm.id ? "save" : "update"
              }`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              code: this.dataForm.code,
              dictCode: this.dataForm.dictCode,
              description: this.dataForm.description,
              active: this.dataForm.active,
              sequence: this.dataForm.sequence,
              editable: this.dataForm.editable,
              parentId: this.categroyPath,
              addTime: this.dataForm.addTime,
              addUser: this.dataForm.addUser,
              editTime: this.dataForm.editTime,
              editUser: this.dataForm.editUser,

            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    dialogClose(){
      this.categroyPath = 0;
    },
  },

};
</script>
